/*
 * Copyright (C) 2018 Logos Technology Ltd.
 */

package com.logos.common.source.network.request;

import com.logos.common.source.network.callback.MultiUploadFileProgressListener;
import com.logos.tool.log.LogUtil;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

/**
 * @author Young Ken
 */

public class UploadFileProcessManager {

    private static final String TAG = "UploadFileProcessManager";
    //private Map<String, RequestBody> mMap;
    private MultiUploadFileProgressListener mListener;
    private long mTotalByteCount;
    private AtomicLong mCurrentByteCount = new AtomicLong(0);
    private String mTempMarker = "";

    public UploadFileProcessManager(Map<String, RequestBody> mapMap,
                                    MultiUploadFileProgressListener listener) {

        if (mapMap == null) {
            throw new IllegalArgumentException("map == null");
        }
        mListener = listener;
        computeTotalByteCount(mapMap);
    }

    /**
     * 更新上传进度
     *
     * @param fileName          文件名字
     * @param incrementBytes    增量byte
     * @param currentBytesCount 当前的上传的byte
     * @param totalBytesCount   一共的byte
     */
    public synchronized void updateSingleProcess(final String fileName,
                                                 final long incrementBytes,
                                                 final long currentBytesCount,
                                                 final long totalBytesCount) {
        Observable.just(0).subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        if (mListener == null) {
                            return;
                        }

                        //finish upload
                        if (mCurrentByteCount.get() == mTotalByteCount) {
                            return;
                        }

                        if (mTempMarker.equals(fileName + currentBytesCount)) {
                            return;
                        }

                        mListener.onSingleUploadProgress(fileName, currentBytesCount,
                                totalBytesCount, currentBytesCount == totalBytesCount);

                        mListener.onAllUploadProgress(mCurrentByteCount.addAndGet(incrementBytes),
                                mTotalByteCount, mCurrentByteCount.get() == mTotalByteCount);
                        mTempMarker = fileName + currentBytesCount;
                    }
                });

    }

    private void computeTotalByteCount(Map<String, RequestBody> mapMap) {

        for (String fileName : mapMap.keySet()) {
            RequestBody requestBody = mapMap.get(fileName);
            try {
                mTotalByteCount += requestBody.contentLength();
            } catch (IOException exception) {
                exception.printStackTrace();
                LogUtil.error(TAG, "RequestBody.contentLength() IOException"
                        + LogUtil.getStackTrace(exception));
            }
        }
    }
}
